<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    class Stack {
      constructor () {
        this.items = []
      }
      push (element) {
        this.items.push(element)
      }
      pop () {
        return this.items.pop()
      }
      peek () {
        return this,items[this.items.length - 1]
      }
      isEmpty () {
        return this.items.length === 0
      }
      size () {
        return this.items.length
      }
      toString () {
        const result = []
        Object.keys(this.items).forEach(index => {
          result.push(this.items[index], '-')
        })
        result.pop()
        return result.join(' ')
      }
    }

    // 十进制转二进制
    function dev2bin (decNumber) {
      const stack = new Stack()
      // 记录余数
      let remainder = 0
      while (decNumber > 0) {
        remainder = decNumber % 2
        decNumber = Math.floor(decNumber / 2)
        stack.push(remainder)
      }

      // 将数据取出
      const result = []
      while (!stack.isEmpty()) {
        result.push(stack.pop())
      }
      return result.join('')
    }
    console.log(dev2bin(10))
    console.log(dev2bin(101))
    console.log(dev2bin(100))
  </script>
</body>
</html>